<?php
namespace admin\controllers;

use common\models\Tags;
use common\models\TagsBook;
use Yii;

/**
 * 标签书籍管理
 */
class TagsBookController extends BaseController
{
    public $modelClass = 'common\models\TagsBook';
    public $searchModelClass = 'admin\models\TagsBookSearch';

    /**
     * @inheritdoc
     */
    public function actions()
    {
        $actions = parent::actions();

        $actions['create']['redirect'] = ['index', 'tag_id' => Yii::$app->request->get('tag_id')];
        $actions['update']['redirect'] = ['index', 'tag_id' => Yii::$app->request->get('tag_id')];

        return $actions;
    }

    /**
     * @inheritdoc
     */
    public function breadcrumbs()
    {
        $breadcrumbs = [];

        $tag = Tags::findOne(Yii::$app->request->get('tag_id'));

        foreach (['index', 'create', 'update'] as $actionID) {
            $breadcrumbs[$actionID][] = ['label' =>  '标签位管理', 'url' => ['tags/index']];
            $breadcrumbs[$actionID][] = ['label' => $tag->tag_name, 'url' => ['index', 'tag_id' => $tag->id]];
            $breadcrumbs[$actionID][] = $this->getPageTitle($actionID);
        }

        return $breadcrumbs;
    }

    /**
     * @inheritdoc
     */
    public function actionButtons()
    {
        return [
            [
                'label'   => '新增标签作品',
                'url'     => ['create', 'tag_id' => Yii::$app->request->get('tag_id')],
                'options' => ['class' => 'btn green'],
            ],
        ];
    }

    public function actionUp()
    {
        $id = Yii::$app->request->get('id');

        $books = TagsBook::find();
        foreach ($books->each() as $book) {
            if ($book->id == $id) { //如果是置顶的分类
                $book->display_order = 255;
            } else {
                $book->display_order > 0 ?  $book->display_order -= 1 :  $book->display_order;
            }
            $book->save();
        }

        $this->back();
    }
}
